"""
author: liuxu
date: 2024.12.3
description: roctx是用于在程序代码插入标记，方便查看某一行代码何时被执行，在json图就可以看到插入的标记
"""
import allure
import pytest
from common.ssh_command import exec_cmd
from base_public.testbase import BasePublic


@allure.feature('roctx测试')
@pytest.mark.high
class TestRoctx(BasePublic):
    """测试roctx功能"""
    def test_roctx_basic(self, test_env):
        """
        --hiptx-trace: 支持roctx相关的功能，支持用户设置标记、timeline
        :return:
        """
        exe_path = self.compile_source_code(src_file_name='roctx_case/roctx_test.cpp',
                                            compile_paras=f"-I {self.rocm_path}/roctracer/include -O0 -g")
        # 使用hipprof执行roctx测例
        ret, output = exec_cmd(f"hipprof --hip-trace --hiptx-trace {exe_path}", cwd=test_env)
        assert ret is True
        assert self.check_trace_output_success(output) is True

        # 检测json文件是否增加了mark的信息
        mark_info = [f"NestedRange{i}" for i in "ABCDE"] + ["StartStopRangeA"]
        json_path = self.get_all_prof_files_info(output, test_env)["json_list"][0]["abspath"]
        for trace_event in self.parse_prof_json(json_path):
            if trace_event.get("name") in mark_info:
                mark_info.remove(trace_event.get("name"))
        assert len(mark_info) == 0, f"The json file does not generate info: {mark_info}"

    # dtk-25.10新增需求
    def test_roctx_kernel(self, test_env):
        """
        --hiptx-trace: 支持roctx+kernel场景
        :return:
        """
        exe_path = self.compile_source_code(src_file_name='roctx_case/fixed_multi_roctx_kernel.cpp',
                                            compile_paras=f"-I {self.rocm_path}/roctracer/include -O0 -g")
        # 使用hipprof执行roctx测例
        ret, output = exec_cmd(f"hipprof --hip-trace --hiptx-trace --hiptx-kernel --trace-args {exe_path}", cwd=test_env)
        assert ret is True
        assert self.check_trace_output_success(output) is True
        #TODO完善检查逻辑
        #核函数vectorAddKernel会和kernel展示连在一块，待排除原因

